package com.kl.drs.util;

import org.apache.http.StatusLine;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Log4jUtils {
	
	public static Logger getDebugLogger() 
	{
		return LogManager.getLogger("debugLogger");
	}
	
	public static Logger getErrLogger() 
	{
		return LogManager.getLogger("errorLogger");
	}

	public static Logger getLoggerByName(String logName) 
	{
		return LogManager.getLogger("logName");
	}
	
	public static void logDebug(String message) 
	{
		if (isDebug()) 
		{
			getDebugLogger().log(Level.DEBUG, message);
		}
	}
	
	public static boolean isDebug() 
	{
		return Log4jUtils.getDebugLogger().isDebugEnabled();
	}
	
	public static void debugJson(String func, Object ojb) 
	{
		if (isDebug()) 
		{
			try 
			{
				Log4jUtils.logDebug(func + "参数|"
						+ new ObjectMapper().writeValueAsString(ojb));
			} 
			catch (JsonProcessingException e)
			{
				Log4jUtils.logError(func, "debugJson异常", e);
			}
		}
	}
	
	public static void logError(String func, String message, Throwable e) 
	{
		getErrLogger().log("invokeKLWebservice", Level.ERROR, message, e);
	}

	public static void logDebug(StatusLine tLine) {
		if (isDebug()) 
		{
			getDebugLogger().log(Level.DEBUG, tLine);
		}
		
	}
}
